/**
 * 七日登录
 */
import {utils} from "../utils/utils";
import {wxUtils} from "../wx/wxUtils";
import {GameVar, GlobalObject} from "../game/GameVar";

const {ccclass, property} = cc._decorator;

class Toggle extends cc.Component {
    private _onText: cc.Node = null;
    private _offText: cc.Node = null;
    private _toggle: cc.Node = null;
    private _on = true;
    onLoad () {
        this._onText = this.node.getChildByName('on');
        this._offText = this.node.getChildByName('off');
        this._toggle = this.node.getChildByName('toggle');
    }

    turn () {
        this._on ? this.setOn() : this.setOff();
        this._on = !this._on;
        let x = this._on ? this._onText.x : this._offText.x;
        this._toggle.runAction(cc.moveTo(0.1, x, this._toggle.y));
    }

    setOn () {
        this._on = true;
        this._toggle.stopAllActions();
        this._toggle.x = this._onText.x;
    }

    setOff () {
        this._on = false;
        this._toggle.stopAllActions();
        this._toggle.x = this._offText.x;
    }
}

@ccclass
export default class Setting extends cc.Component {

    @property(cc.Label)
    nameLabel: cc.Label = null;

    @property(cc.Label)
    idLabel: cc.Label = null;

    @property(cc.Sprite)
    avatarImage: cc.Sprite = null;

    private _musicToggle: Toggle = null;
    private _effectToggle: Toggle = null;

    start () {
        // 信息
        let uid = GameVar.userInfo.uid && GameVar.userInfo.uid.toString();
        this.idLabel.string = uid;
        this.nameLabel.string = GameVar.userInfo.nick;
        wxUtils.getHeadTexture(this.avatarImage);
        // 设置
        let bg = this.node.getChildByName('bg');
        utils.dialogPopup(this.node, bg);
        this._musicToggle = bg.getChildByName('button_music').addComponent(Toggle);
        this._effectToggle = bg.getChildByName('button_effect').addComponent(Toggle);

        GlobalObject.voiceSource.muteBgm ? this._musicToggle.setOff() : this._musicToggle.setOn();
        GlobalObject.voiceSource.muteEffect ? this._effectToggle.setOff() : this._effectToggle.setOn();

        let block = this.node.getChildByName('block');  // 屏蔽点击的全屏黑色半透背景
        let content = bg.getChildByName('content');  // 点击content外部关闭，bg的空白像素范围有点大
        block.on(cc.Node.EventType.TOUCH_END, (event: cc.Event.EventTouch) => {
            let p = content.convertTouchToNodeSpace(event.touch);
            if (p.x<0 || p.x>content.width || p.y<0 || p.y>content.height) {
                this.close();
            }
        }, this);
        // 复制id
        /*
        this.idLabel.node.on(cc.Node.EventType.TOUCH_END, function(){
            wx.setClipboardData({
                data: uid,
                success: function(res) {
                    wx.getClipboardData({
                        success: function(res) {
                            cc.log("复制成功：", res.data);
                        }
                    });
                }
            });
        });
        */
    }

    changeMusic () {
        this._musicToggle.turn();
        GlobalObject.voiceSource.muteBgm = !GlobalObject.voiceSource.muteBgm;
    }

    changeEffect () {
        this._effectToggle.turn();
        GlobalObject.voiceSource.muteEffect = !GlobalObject.voiceSource.muteEffect;
    }

    close () {
        utils.dialogClose(this.node, this.node.getChildByName('bg'));
    }

}
